package com.lhy.goods.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhy.domain.Spec;
import com.lhy.entity.PageResult;
import com.lhy.entity.Result;
import com.lhy.entity.StatusCode;
import com.lhy.goods.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 规格参数与分类管理
*/
@RestController
@RequestMapping("/Spec")
public class SpecController {
    @Autowired
    private SpecService specService;
    /**
     * 查全部
     */
    @GetMapping("/selectAll")
    public Result<List<Spec>> selectAll() {
        return new Result<>("查询成功", specService.selectAll());
    }
    /**
     * 新增
     */
    @PostMapping("/insert")
    public Result<Integer> insert(@RequestBody Spec spec) {
        return new Result<>("插入成功", specService.insert(spec));
    }
    /**
     * 更新数据
     */
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody Spec spec) {
        return new Result<>("更新成功", specService.update(spec));
    }
    /**
     * 根据id删除一/多条数据
     */
    @PostMapping("/delete")
    public Result<Integer> delete(@RequestParam String ids) {
        Integer delete = specService.delete(ids);
        if (delete==1){
            return new Result<>("删除成功",delete );
        }
        return new Result<>("删除失败",delete );
    }
    /**
     * 条件查询
     */
    @PostMapping("/selectList")
    public Result<List<Spec>> select(@RequestBody Spec spec) {
        return new Result<>("条件查询成功", specService.selectList(spec));
    }
    /**
     * 分页查询
     */
    @GetMapping(value = "/pageAll")
    public Result<PageResult<Spec>> pageAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<Spec> pageList = specService.pageAll(page, size);
        PageResult<Spec> pageResult = new PageResult<>(pageList.getTotal(), pageList.getRecords());
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }
    /**
     * 分页+条件查询
     */
    @GetMapping("/pageList")
    public Result<PageResult<Spec>> pageList(@RequestBody Spec spec, @RequestParam(defaultValue = "1") Integer current, @RequestParam Integer size) {
        Page<Spec> pageList = specService.pageList(spec, current, size);
        PageResult<Spec> pageResult = new PageResult<>(pageList.getTotal(), pageList.getRecords());
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }


}
